﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace MLY.LTR
{
  public partial class SubContractorCard : Bll.BasePage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      Response.Expires = 0;
      Response.Cache.SetNoStore();
      Response.AppendHeader("Pragma", "no-cache");

      lblSuccessfullSaving.Visible = false;
      lblRelationExistsErr.Visible = false;

      if (!Page.IsPostBack)
      {
        ViewState["Referer"] = Request.ServerVariables["HTTP_REFERER"];
        int clientId = SessParam.OrganizationId;
        lblRelationExistsErr.Visible = false;
        lblSuccessfullSaving.Visible = false;

        int ContractorId;
        if ((clientId > 0) &&
            g.ParseQueryString(Request.QueryString, "ContractorId", out ContractorId))
        {       
          if (ContractorId != -1)
          {
            subContractorRow.Visible = false;
            Bll.clsOrganization con = new MLY.Bll.clsOrganization(ContractorId);
            Bll.clsOrganizationRelation rel = new MLY.Bll.clsOrganizationRelation(clientId, ContractorId);

            this.lblContactPersonName.Text = con.Data.ContactPerson;
            this.lblContactPersonEmail.Text = con.Data.ContactPersonEmail;
            this.lblContactPersonPhone.Text = con.Data.ContactPersonPhone;
            this.txtContractorCode.Text = rel.Data.ContarctorCode;
            this.txtDefaultCost.Text = g.FormatDecimalAmount(rel.Data.DefaultCost);
            this.txtDefaultPeriodDays.Text = rel.Data.DefaultTimeDurationDays.ToString();
            this.txtDeviationParam.Text = rel.Data.DefaultDeviationParameter.ToString();
          }
          else subContractorRow.Visible = true;    
        }
      }
    }

    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
      //Response.Write("<script language = \"Javascript\"> alert(\"לתיק זה כבר נרשמה הפניה לועדה.\");</script>");
      int clientId = SessParam.OrganizationId;
      Bll.clsOrganization client, contractor;

      int ContractorId;
      if ((clientId > 0) &&
          g.ParseQueryString(Request.QueryString, "ContractorId", out ContractorId))
      {

        if (ContractorId != -1)
        {
          Bll.clsOrganizationRelation rel = new MLY.Bll.clsOrganizationRelation(clientId, ContractorId);

          rel.Data.ContarctorCode = this.txtContractorCode.Text;
          rel.Data.DefaultCost = g.ParseDecimal(this.txtDefaultCost.Text);
          rel.Data.DefaultTimeDurationDays = g.ParseDecimal(this.txtDefaultPeriodDays.Text);
          rel.Data.DefaultDeviationParameter = g.ParseInt(this.txtDeviationParam.Text);
          rel.Save();

          client = new MLY.Bll.clsOrganization(clientId);
          contractor = new MLY.Bll.clsOrganization(ContractorId);

          lblSuccessfullSaving.Visible = true;
          lblRelationExistsErr.Visible = false;
        }
        else
        {
          // Do not allow to insert a new record to TableOrganizationRelations for an existing relation

          Bll.clsOrganizationRelation rel = new MLY.Bll.clsOrganizationRelation();

          rel.Data.CustomerId = clientId;
          rel.Data.ContractorId = int.Parse(ddlSubContractorsList.SelectedValue);
          rel.Data.ContarctorCode = this.txtContractorCode.Text;
          rel.Data.DefaultCost = g.ParseDecimal(this.txtDefaultCost.Text);
          rel.Data.DefaultTimeDurationDays = g.ParseDecimal(this.txtDefaultPeriodDays.Text);
          rel.Data.DefaultDeviationParameter = g.ParseInt(this.txtDeviationParam.Text);
          try
          {
            rel.Save();
          }
          catch //(Exception ex)
          {
            lblSuccessfullSaving.Visible = false;
            lblRelationExistsErr.Visible = true;
            return;
          }
          client = new MLY.Bll.clsOrganization(rel.Data.CustomerId.Value);
          contractor = new MLY.Bll.clsOrganization(rel.Data.ContractorId.Value);
          lblSuccessfullSaving.Visible = true;
          lblRelationExistsErr.Visible = false;

        }
        string subject = "MLY automatic system massage";
        string content = String.Format(@"Hello  <br />
          This is automatic system massage <br />
          Your organization's connection with {0} was established or updated. <br /> 
          Best Regards, <br />
          MLY team", client.Data.OrganizationName);

        Utils.mlySendMail(subject,
             contractor.Data.ContactPersonEmail + "," + client.Data.ContactPersonEmail + "," + Utils.AdminMail, "", "", content, true, MLY.Utils.MailTemplate.AddContractor);
      }
    }

    protected void btnClose_Click(object sender, ImageClickEventArgs e)
    {
        if ((string)ViewState["Referer"] != null)
            Response.RedirectTo((string)ViewState["Referer"]);
        else
            Response.Redirect("~/LTR/subcontractors.aspx?p=1.1.2.1");
    }

    protected void ddlSubContractorsList_SelectedIndexChanged(object sender, EventArgs e)
    {
      Bll.clsOrganization con = new MLY.Bll.clsOrganization(int.Parse(ddlSubContractorsList.SelectedValue));
      this.lblContactPersonName.Text = con.Data.ContactPerson;
      this.lblContactPersonEmail.Text = con.Data.ContactPersonEmail;
      this.lblContactPersonPhone.Text = con.Data.ContactPersonPhone;
    }
  }
}
